<template>
  <div class="container">
    <div class="app-container">
      <!-- 展示树形结构 -->
      <el-tree :expand-on-click-node="false" default-expand-all :data="depts" :props="defaultProps">
        <!-- 节点结构 -->
        <!-- v-slot="{ node, data }" 只能作用在template -->
        <template v-slot="{ data }">
          <el-row style="width:100%;height:40px" type="flex" justify="space-between" align="middle">
            <el-col>{{ data.name }}</el-col>
            <el-col :span="4">
              <span class="tree-manager">{{ data.managerName }}</span>
              <!-- $event 实参 表示类型 -->
              <el-dropdown @command="operateDept($event, data.id)">
                <!-- 显示区域内容 -->
                <span class="el-dropdown-link" style="margin-left:35px">
                  操作<i class="el-icon-arrow-down el-icon--right" />
                </span>
                <!-- 下拉菜单选项 -->
                <el-dropdown-menu slot="dropdown">
                  <el-dropdown-item command="add" @click.native="add(data.id)">添加子部门</el-dropdown-item>
                  <el-dropdown-item command="edit" @click.native="edit(data.id)">编辑部门</el-dropdown-item>
                  <el-dropdown-item command="del" @click.native="del(data.id)">删除</el-dropdown-item>
                </el-dropdown-menu>
              </el-dropdown>
            </el-col>
          </el-row>
        </template>
      </el-tree>
      <depDg ref="dept" :show-dialog.sync="isShow" :dep-id="curId" @updateEvent="getDepartment" />
    </div>
    <!-- 放置弹层 -->
    <!-- 表示会接受子组件的事件  update:showDialog, 值 => 属性 -->

  </div>
</template>
<script>
import { departmentList, deleteDept } from '@/api/department'
import { transformTreeData } from '@/utils/index'
import depDg from './department/depDg.vue'

export default {
  name: 'Department',
  // 注意components不要写错
  components: {
    depDg
  },

  data() {
    return {
      depts: [
        {
          name: '传智博客',
          children: [
            {
              name: '人力资源'
            }
          ]
        }
      ],

      defaultProps: {
        label: 'name', // 要显示的字段的名字
        children: 'children' // 读取子节点的字段名
      },
      isShow: false,
      curId: null
    }
  },
  created() {
    this.getDepartment() // 调用获取数据的接口
  },
  methods: {
    // 封装好方法
    async getDepartment() {
      this.depts = await departmentList()
      const res = transformTreeData(this.depts, 0)
      console.log('转换后的结构', res)
    },
    // 操作部门方法
    add(id) {
      this.curId = id
      console.log('触发了')
      this.isShow = true
      console.log(this.isShow, 83)
    },
    async  del(id) {
      console.log(id, 88)
      await deleteDept(id)
      this.$message.success('删除成功')
      this.getDepartment()
    },
    edit(id) {
      this.add(id)
      this.$nextTick(() => {
        console.log(this.$refs.dept, 96)
        this.$refs.dept.getDtailList()
      })
    }

  }
}
</script>
<style scoped>
.app-container {
  padding: 30px 140px;
  font-size: 14px;
}
.tree-manager {
  width: 50px;
  display: inline-block;
  margin: 10px;
}
</style>
